// 生产环境
var ApiDoName = 'http://580fe044.nat123.fun/'
var ApiDoNameSocket = 'http://580fe044.nat123.fun/'
var ApiDoNameShop = ApiDoName+'/shop-server/v001/'
var ApiDoNameAuth = ApiDoName+'/auth-server/v001/'
var ApiDoNameCommon = ApiDoName+'/common-server/v001/'

// 开发环境
if (location.hostname.indexOf('tinydev.cc')>-1){
  ApiDoName = 'http://tinymall-api.tinydev.cc'
  ApiDoNameSocket = 'http://tinymall-api.tinydev.cc'
  ApiDoNameShop=ApiDoNameShop.replace('cn','cc')
  ApiDoNameAuth=ApiDoNameAuth.replace('cn','cc')
  ApiDoNameCommon=ApiDoNameCommon.replace('cn','cc')
}


// 本地环境
if (location.hostname.indexOf('127.0.0.1')>-1 || location.hostname.indexOf('localhost')>-1){
  ApiDoName='http://127.0.0.1:9200'
  ApiDoNameShop = ApiDoName + '/shop-server/v001/'
  ApiDoNameAuth = ApiDoName + '/auth-server/v001/'
  ApiDoNameCommon = ApiDoName + '/common-server/v001/'
  ApiDoNameSocket = 'http://127.0.0.1:9092'
}




window._AMapSecurityConfig = {
  // serviceHost:'您的代理服务器域名或地址/_AMapService',
  securityJsCode:'a8637f8fc757e27b95a3f81a79cbd1c0'
  // 例如 ：serviceHost:'http://1.1.1.1:80/_AMapService',
}

var MyCartTools={}
MyCartTools.localStorageKey="TinyMall_CartCache"
MyCartTools.setAllList=function (allList) {
  var _allList=[]
  allList.forEach(function (item,index) {
    _allList.push({
      supplier:{
        id:item.supplier.id,
        name:item.supplier.name,
        logo:item.supplier.logo,
      },
      productList:item.productList,
    })
  })
  localStorage.setItem(MyCartTools.localStorageKey,JSON.stringify(_allList))
}
MyCartTools.getAllList=function () {
  var allProductList = []
  var cacheString = localStorage.getItem(MyCartTools.localStorageKey)
  if (cacheString) {
    allProductList = JSON.parse(cacheString)
  }
  return allProductList;
}
// 商品-增加
MyCartTools.addProduct=function (productParams) {
  console.log('productParams',productParams)
  var allProductList =this.getAllList()
  var currShopIndex = -1
  var currProductIndex = -1
  allProductList.forEach(function (item, index) {
    if (productParams.supplierId === item.supplier.id) {
      currShopIndex = index
      item.productList.forEach(function (_item, _index) {
        if (_item.id === productParams.id && _item.model===productParams.model) {
          currProductIndex = _index
        }
      })
    }
  })

  // 如果店铺是空
  if (currShopIndex === -1) {
    allProductList.push({
      supplier: {
        id: productParams.supplierId,
        logo: productParams.supplierLogo,
        name: productParams.supplierName,
      },
      productList: [
        {
          id: productParams.id,
          productId: productParams.productId,
          image: productParams.image,
          name: productParams.name,
          price: productParams.price,
          model: productParams.model,
          nums: productParams.nums
        }
      ]
    })
  } else {
    if (currProductIndex === -1) {
      allProductList[currShopIndex].productList.push({
        id: productParams.id,
        image: productParams.image,
        productId: productParams.productId,
        name: productParams.name,
        price: productParams.price,
        model: productParams.model,
        nums: productParams.nums
      })
    } else {
      allProductList[currShopIndex].productList[currProductIndex].nums++
    }
  }
  localStorage.setItem(MyCartTools.localStorageKey, JSON.stringify(allProductList))
}
// 商品-减少
MyCartTools.subProduct=function (productId) {
  var allProductList =this.getAllList()
  var currShopIndex = -1
  var currProductIndex = -1
  allProductList.forEach(function (item, index) {
    if (productParams.supplierId === item.supplier.id) {
      currShopIndex = index
      item.productList.forEach(function (_item, _index) {
        if (_item.id === productParams.id && _item.model===productParams.model) {
          currProductIndex = _index
        }
      })
    }
  })

  // 如果店铺是空
  if (currShopIndex !== -1 && currProductIndex !== -1) {
    allProductList[currShopIndex].productList[currProductIndex].nums--
  }
  var realProductList = []
  allProductList.forEach(function (item, index) {
    var tmpList = []
    item.productList.forEach(function (_value, _index) {
      if (_value.nums !== 0) {
        tmpList.push(_value)
      }
    })
    // 如果有商品列表,才会压入正式数组
    if (tmpList.length > 0) {
      realProductList.push({
        supplier: item.supplier,
        productList: tmpList,
      })
    }
  })
  localStorage.setItem(MyCartTools.localStorageKey, JSON.stringify(realProductList))
}
// 删除-商品
MyCartTools.delProduct=function (productId,model) {
  var allProductList =this.getAllList()
  var realProductList = []
  allProductList.forEach(function (item, index) {
    var tmpList = []
    item.productList.forEach(function (_item, _index) {
      if (!(_item.id === productId && _item.model===model)) {
        tmpList.push(_item)
      }
    })
    if (tmpList.length > 0) {
      realProductList.push({
        supplier: item.supplier,
        productList: tmpList,
      })
    }
  })
  localStorage.setItem(MyCartTools.localStorageKey, JSON.stringify(realProductList))
}
// 删除-店铺
MyCartTools.delSupplier=function (supplier) {
  var allProductList =this.getAllList()
  var tmpAllList=[]
  allProductList.forEach(function (item, index) {
    if (item.supplier.id!==supplier){
      tmpAllList.push(item)
    }
  })
  localStorage.setItem(MyCartTools.localStorageKey, JSON.stringify(tmpAllList))
}
// 删除-商品
MyCartTools.deleteItem=function (productId, productList) {
  var arr = []
  productList.forEach(function (value, index) {
    if (productId !== value.id) {
      arr.push(value)
    }
  })
  return arr
}


var TinyNavArr=[
  {
    name: '主页',
    icon: 'el-icon-goods',
    subMenu: [
      {
        name: '主页',
        url: 'Home',
        action: '',
        icon: ''
      },
      {
        name: '聊天记录',
        url: 'ChatPage',
        action: '',
        icon: 'el-icon-setting'
      },
    ]
  },
  {
    name: '店铺',
    icon: 'el-icon-goods',
    subMenu: [
      {
        name: '店铺-信息',
        url: 'ShopPage',
        action: 'Shop/info',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺-更新资料',
        url: 'ShopUpdateMainPage',
        action: 'Shop/updateInfo',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺-绑定-师傅',
        url: 'ShopBindingMemberPage',
        action: 'Shop/getBindingMemberList',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺-银行卡',
        url: 'ShopBankPage',
        action: 'ShopBank/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺-提现',
        url: 'ShopWithdrawPage',
        action: 'ShopWithdraw/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺-店员',
        url: 'SysUser',
        action: 'User/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺-余额日志',
        url: 'ShopMoneyLogPage',
        action: 'Shop/getMoneyLogList',
        icon: ''
      }
    ]
  },
  {
    name: '商品',
    icon: 'el-icon-goods',
    subMenu: [
      {
        name: '商品-列表',
        url: 'ProductPage',
        action: 'Product/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '商品日志-列表',
        url: 'ProductLogPage',
        action: 'Product/getLogList',
        icon: 'el-icon-setting'
      },
      {
        name: '商品型号-列表',
        action: 'Product/getStockList',
        url: 'ProductStockPage',
        icon: 'el-icon-setting'
      },
      {
        name: '商品型号日志-列表',
        url: 'ProductStockLogPage',
        action: 'Product/getStockLogList',
        icon: 'el-icon-setting'
      },
      {
        name: '商品规格-列表',
        url: 'ShopProductSpecificationsPage',
        action: 'ShopProductSpecifications/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '商品抢购-列表',
        url: 'ShopSkillProductPage',
        action: 'ShopSkill/getList',
        icon: 'el-icon-setting'
      },
    ]
  },
  {
    name: '订单',
    icon: 'el-icon-goods',
    subMenu: [
      {
        name: '订单-列表',
        url: 'OrderPage',
        action: 'Order/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '团购订单-列表',
        url: 'OrderGroupPage',
        action: 'Order/getGroupList',
        icon: 'el-icon-setting'
      },
      {
        name: '订单退换货-列表',
        url: 'OrderApplyPage',
        action: 'OrderApply/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '订单评价-列表',
        url: 'OrderReplyPage',
        action: 'OrderReply/getList',
        icon: ''
      },
      {
        name: '订单物流-列表',
        url: 'OrderExpressPage',
        action: 'Order/getExpressList',
        icon: ''
      }
    ]
  },
  {
    name: '供货',
    icon: 'el-icon-goods',
    subMenu: [
      {
        name: '供货商',
        url: 'SupplierSearchPage',
        action: 'Supplier/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '供货-商品',
        url: 'SupplierProductSearchPage',
        action: 'SupplierProduct/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '供货-订单',
        url: 'SupplierOrderListPage',
        action: 'SupplierOrder/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '供货-退换货订单',
        url: 'SupplierOrderApplyListPage',
        action: 'SupplierOrderApply/getList',
        icon: 'el-icon-setting'
      },
    ]
  },
  {
    name: '其他',
    icon: 'el-icon-goods',
    subMenu: [
      {
        name: '地址管理',
        url: 'ShopAddressPage',
        action: 'ShopAddress/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺公告',
        url: 'ShopNoticePage',
        action: 'ShopNotice/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '店铺优惠券',
        url: 'CouponPage',
        action: 'Coupon/getList',
        icon: 'el-icon-setting'
      },
      {
        name: '登录日志',
        url: 'UserLoginLogPage',
        action: 'Logs/getUserLoginLogList',
        icon: 'el-icon-setting'
      },
    ]
  },
]

window.getNavDataByPath=function(currUrl) {
  var currItem={}
  TinyNavArr.forEach(function (item,index) {
    item.subMenu.forEach(function (_item,_index) {
      if (_item.url===currUrl){
        currItem=_item
      }
    })
  })
  return currItem
}


window.AppVersion="v1.0"
